package com.xc.common.tool;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class ZkCommandSender {
    protected void TCPClient(String command) {
        Socket socket=null;
        try {
            socket = new Socket("192.168.3.253", 8932);
            //获取输出流，向服务器发送数据
            OutputStream os = socket.getOutputStream();
            InputStream in = socket.getInputStream();
            byte b[] = parseHexStr2Byte(command);
            os.write(b);
            os.flush();
            socket.shutdownOutput();
            os.close();
            in.close();
            socket.close();
        } catch (Exception ex) {
            try {
                if (socket != null){
                    socket.close();
                }
            } catch (Exception ex1){}
        }
    }
    public void MainWork(String ip,int port,String command) {
        TCPClient(command);
    }
    private byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1) {
            return null;
        }
        byte[] result = new byte[hexStr.length() / 2];
        for (int i = 0; i < hexStr.length() / 2; i++) {
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }
}
